// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngMessages', 'ionic-datepicker'])
    // bower install angular-mocks --save
    // <script src="lib/angular-mocks/angular-mocks.js"></script>
    // https://docs.angularjs.org/api/ngMockE2E
    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
            // for form inputs)
            if (window.cordova && window.cordova.plugins.Keyboard) {
                cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            }
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
        });
    })

    .config(function ($stateProvider, $urlRouterProvider, USER_ROLES, $httpProvider) {

        $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

        $stateProvider
            .state('login', {
                url: '/login',
                templateUrl: 'templates/login.html',
                controller: 'LoginCtrl'
            })
            .state('main', {
                url: '/',
                abstract: true,
                templateUrl: 'templates/main.html'
            })
            .state('main.house', {
                url: 'house',
                views: {
                    'house-tab': {
                        templateUrl: 'templates/house/house.html',
                        controller: 'HouseCtrl'
                    }
                }
            })
            .state('main.house-entire', {
                url: 'house/entire',
                views: {
                    'house-tab': {
                        templateUrl: 'templates/house/house-entire.html',
                        controller: 'HouseCtrl'
                    }
                }
            })
            .state('main.public', {
                url: 'main/public',
                views: {
                    'public-tab': {
                        templateUrl: 'templates/public.html'
                    }
                }
            })
            .state('main.admin', {
                url: 'main/admin',
                views: {
                    'admin-tab': {
                        templateUrl: 'templates/admin.html'
                    }
                },
                data: {
                    authorizedRoles: [USER_ROLES.admin]
                }
            });
        $urlRouterProvider.otherwise('/login');
    })


    .run(function ($ionicPlatform, $rootScope, $state, $location, AuthService, AUTH_EVENTS) {
        $rootScope.$on('$stateChangeStart', function (event, next, nextParams, fromState) {

            if ('data' in next && 'authorizedRoles' in next.data) {
                var authorizedRoles = next.data.authorizedRoles;
                if (!AuthService.isAuthorized(authorizedRoles)) {
                    event.preventDefault();
                    $state.go($state.current, {}, {reload: true});
                    $rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
                }
            }

            if (!AuthService.isAuthenticated()) {
                if (next.name !== 'login') {
                    event.preventDefault();
                    $state.go('login');
                }
            }
        });
    })
    // 配置日期插件
    .config(function (ionicDatePickerProvider) {
        var datePickerObj = {
            // 日期空间上面的日期格式
            dateFormat: 'yyyy-MM-dd',
            closeLabel: '取消',
            weeksList: ["日", "一", "二", "三", "四", "五", "六"],
            monthsList: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            mondayFirst: true,
            closeOnSelect: true,
            templateType: 'popup',
            // disableWeekdays:[0] 用于禁用一周中的星期几
        };
        ionicDatePickerProvider.configDatePicker(datePickerObj);
    })
;
